package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import model.Behandling;
import model.DelBehandling;
import model.DrageringBehandling;
import model.LJButton;
import model.LagerPlads;
import model.Mellemvare;
import model.ProduktType;
import model.ToerretidBehandling;
import service.Service;
import javax.swing.SwingConstants;

/**
 * 
 * @author Lars
 *
 */

public class LagerOversigtFrame extends JDialog {

	private JPanel contentPane;
	private JPanel pnlLagerOversigt;
	private JPanel oversigtContainer;
	
	private ButtonOversigtListener buttonOversigtListener = new ButtonOversigtListener();
	
	/**
	 * Create the frame.
	 */
	public LagerOversigtFrame() {
		setTitle("Lageroversigt");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.GREEN);
		panel.add(panel_2);
		
		JLabel lblOptimalTid = new JLabel("Optimal tid");
		panel_2.add(lblOptimalTid);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.YELLOW);
		panel.add(panel_3);
		
		JLabel lblMinTid = new JLabel("Min. tid");
		panel_3.add(lblMinTid);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.RED);
		panel.add(panel_4);
		
		JLabel lblMaxTid = new JLabel("Max. tid");
		panel_4.add(lblMaxTid);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBackground(Color.GRAY);
		panel.add(panel_5);
		
		JLabel lblForGammel = new JLabel("For gammel");
		panel_5.add(lblForGammel);
		
		pnlLagerOversigt = new JPanel();
		contentPane.add(pnlLagerOversigt, BorderLayout.CENTER);
		
		oversigtContainer = new JPanel();
		
		GroupLayout gl_pnlLagerOversigt = new GroupLayout(pnlLagerOversigt);
		gl_pnlLagerOversigt.setHorizontalGroup(
			gl_pnlLagerOversigt.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlLagerOversigt.createSequentialGroup()
					.addGap(80)
					.addComponent(oversigtContainer, GroupLayout.PREFERRED_SIZE, 270, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(80, Short.MAX_VALUE))
		);
		gl_pnlLagerOversigt.setVerticalGroup(
			gl_pnlLagerOversigt.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlLagerOversigt.createSequentialGroup()
					.addContainerGap()
					.addComponent(oversigtContainer, GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
					.addContainerGap())
		);
		pnlLagerOversigt.setLayout(gl_pnlLagerOversigt);
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		contentPane.add(panel_1, BorderLayout.SOUTH);
		
		JButton btnLuk = new JButton("Luk");
		btnLuk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		panel_1.add(btnLuk);	
		generateLagerOversigt();
	}
	
	private void generateLagerOversigt() {
		oversigtContainer.removeAll();
		
		for(LagerPlads lp : Service.getInstance().getLagerPladser()) {
			Mellemvare m = lp.getMellemvare();
			
			LJButton b = new LJButton(lp);
			b.setMargin(new Insets(2,2,2,2));
			b.setPreferredSize(new Dimension(70, 25));
			
			if(m != null) {
				b.setText(m.getId()+"");
				
				switch(m.getNewestTimeStamp().getDelBehandling().getPlukkeklar()) {
				case OPTIMAL:
					b.setBackground(Color.GREEN);
					break;
				case MIN:
					b.setBackground(Color.YELLOW);
					break; 
				case MAX:
					b.setBackground(Color.DARK_GRAY);
					break;
				case FORGAMMEL:
					b.setBackground(Color.RED);
					break;
				default:
					b.setEnabled(false);
					break;
				}
			}
			else {
				b.setText("Ledig");
				b.setEnabled(false);
			}
			
			b.addActionListener(buttonOversigtListener);
			oversigtContainer.add(b);
		}
		
		oversigtContainer.revalidate();
	}
	
	private class ButtonOversigtListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			LJButton btn = (LJButton)e.getSource();
			
			LagerRedigerLagerplads lrl = new LagerRedigerLagerplads(btn.getLagerplads());
			lrl.setVisible(true);
			
			generateLagerOversigt();
		}
		
	}
}
